package com.neolix.watch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.neolix.watch.adapter.ImageAdapter;
import com.neolix.watch.bluetooth.BlueToothUtil;
import com.neolix.watch.view.GalleryFlow;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class WatchStyleActivity extends NeolixNaviagationBaseActivity {
    private static final String TAG = WatchStyleActivity.class.getSimpleName();
    private ImageSwitcher imageSwitcher;
    private View commitView;
    private View startTextViw;
    private View selectImageView;
    private int watchStyle;

    public WatchStyleActivity() {
        super(R.layout.activity_watch_style);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getString(R.string.time_style));
        commitView = findViewById(R.id.bottom_view);
        startTextViw = findViewById(R.id.start_text_view);
        selectImageView = findViewById(R.id.select_image_view);

        commitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectWatch();
            }
        });

        startTextViw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectWatch();
            }
        });
        initImageSwitcher();
        initGalleryFlow();
    }

    private void selectWatch() {
        BlueToothUtil.syncWatchStyle(watchStyle);
        startTextViw.setVisibility(GONE);
        selectImageView.setVisibility(VISIBLE);
    }

    private void initGalleryFlow() {
        Integer[] images = {R.drawable.ic_screen_1, R.drawable.ic_screen_2,
                R.drawable.ic_screen_3};

        final ImageAdapter adapter = new ImageAdapter(this, images);
        adapter.createReflectedImages();

        GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.gallery_flow);
        galleryFlow.setSpacing(5);
        galleryFlow.setAdapter(adapter);
        galleryFlow.setSelection(0);

        galleryFlow.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                watchStyle = i;
                imageSwitcher.setImageResource(adapter.getItem(i));
                startTextViw.setVisibility(VISIBLE);
                selectImageView.setVisibility(GONE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    private void initImageSwitcher() {
        imageSwitcher = (ImageSwitcher) findViewById(R.id.watch_style_image);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(WatchStyleActivity.this);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
                return imageView;
            }
        });

    }
}
